<script>
	// #ifdef H5
	// import pageAnimation from './components/page-animation'
	// #endif
	// #ifdef APP-PLUS
	import APPUpdate from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
	// #endif
	import * as userApi from '@/api/user'
	import util from '@/utils/utils.js'
	import * as Api from '@/api/home.js'
	import * as goodsApi from '@/api/good_detail.js'
	export default {
		// #ifdef H5
		// mixins: [pageAnimation],
		// #endif
		data(){
			return{
				isCallback:false
			}
		},
		onShow() {
			// #ifdef APP-PLUS
			setTimeout(() => {
				uni.getNetworkType({
					success:function(res){
							if (res.networkType == 'none') {
									uni.showModal({
											title: '网络链接失败',
											content: '检测到网络权限可能设置为关闭，您可以在“设置”中检查无线数据及蜂窝移动网络',
											success: function (res) {
												plus.runtime.restart()
											}
									})
							}
					}
				})
			},1000)
			// #endif
		},
		onLaunch: function(option) {
			// #ifdef APP-PLUS
			APPUpdate()
			// #endif
			this.appid()
			this.getContactUs()
			this.getooodscommonl()
			// #ifdef MP-WEIXIN
			if(option.query.yq && option.query.yq!="undefined"){
				uni.setStorageSync('yq', option.query.yq)
			}
			this.getstatus()
			this.shareMp()
			// #endif
			let _this = this
			if(option.path === 'pages/user/setup/bindaccount'){
				this.$isResolve()
				return
			} 
			// this.logouttype()
			// this.getDetail()
			// #ifdef H5
			let token = uni.getStorageSync("AccessToken")
			let cache_token = uni.getStorageSync('AccessToken')
			const oldCode = uni.getStorageSync('wechatCode')
			userApi.check_token({
				wxapp_id: this.$wxapp_id,
				token:token,
			}).then(res => {
				if(!res){
					uni.clearStorageSync()
					this.$isResolve()
					// if(util.checkBrowser()) {
					// 	this.wxLogin()
					// }else {
					// 	this.$isResolve()
					// }
				}else{
					this.userInfo()
					this.$isResolve()
				}
			
			}).catch(e =>{
			
			})
			// #endif
		},
		methods:{
			logouttype(){
				let token = uni.getStorageSync("AccessToken")
				if(!token){
					return
				}
				userApi.check_token({
					token
				})
				.then(res => {
					if(!res){
						uni.clearStorageSync()
						return
					}else{
						userApi.logouttype()
						.then(result => {
							if(Number(result.data.is_delete) != 1){
								uni.redirectTo({
									url:"/pages/logout"
								})
							}
						})
					}
				})
			},
			getooodscommonl(){
				let userInfo = uni.getStorageSync("userInfo")
				goodsApi.getooodscommonl({
					user_id:userInfo.user_id
				})
				.then(result => {
					if(result.data){
						uni.setStorageSync('commonList',result.data)
					}
				})
			},
			wxLogin(){
				let code = this.$route.query.code
				let yq = this.$route.query.yq
				if(code){
					userApi.authLogin({
						wxapp_id: this.$wxapp_id,
						code,
						yq
					}).then(res => {
						if(res.code == 1){
							uni.setStorageSync("AccessToken",res.data.token)
							uni.removeStorageSync('refresh')
							uni.setStorageSync('wechatCode', code)
							this.userInfo()
							this.$isResolve()
						}else if (res.code === 0){
							uni.setStorageSync('wechatCode', code)
							this.$isResolve()
						}
					})
				}else{
					util.checkgetCode()
				}
			},
			getDetail(){
				return new Promise((relove,reject) => {
					Api.homefloating()
					.then(result => {
						console.log(result)
						relove(result)
						
					})
				})

			},
			// 处理微信回调
			onCallback() {
			 
			 let code = this.checkgetUrlParam('code')
			 let yq = this.checkgetUrlParam('yq')
			 if(!code){
				 this.isCallback = false
				 return
			 }
			  // 请求后端获取微信用户信息
			  this.isCallback = true
				userApi.authLogin({
					wxapp_id: this.$wxapp_id,
					code,
					yq
				}).then(res => {
					if(res.code == 1){
						uni.setStorageSync("AccessToken",res.data.token)
						uni.removeStorageSync('refresh')
						uni.setStorageSync('wechatCode', code)
						this.userInfo()
						this.$isResolve()
					}else if (res.code === 0){
						uni.setStorageSync('wechatCode', code)
						this.$isResolve()
					}
				})
			},
			redirectUrl() {
			  if (this.isCallback) {
			    return
			  }
				let num = uni.getStorageSync('refresh')
				if(!num || num <= 2){
					uni.setStorageSync('refresh',num?num+1:1)
					util.checkgetCode()
				}else{
					this.$isResolve()
					uni.navigateTo({
						url:"/pageace_cart/pages/login/index"
					})
				}
			},
			checkgetUrlParam(name){
				let url = window.location.href;
				let requestParams = {};
				if (url.indexOf("?") !== -1) {
					let str = url.substr(url.indexOf("?") + 1); //截取?后面的内容作为字符串
					let strs = str.split("&"); //将字符串内容以&分隔为一个数组
					for (let i = strs.length-1; i >= 0 ; i--) {
						if(decodeURI(strs[i].split("=")[0]) == name){
							// resolve();
							return decodeURI(strs[i].split("=")[1])
						}else{
							if(strs[i].indexOf(name+'%3D') != -1){
								let list = strs[i].split(name+'%3D')
								return list[1]
							}
						}
					}
				}
				return ""
			},
			appid(){
				let _this = this
				userApi.app_wx({
					wxapp_id: _this.$wxapp_id,
				}).then(res => {
					uni.setStorageSync('appid',res.data)
				})
			},
			getContactUs() {
				let _this = this;
				let token = uni.getStorageSync("AccessToken")
				userApi.connection({
						wxapp_id: _this.$wxapp_id,
						token: token,
					})
					.then(result => {
						let url = util.checkserviceUrl(result.data.link)
						uni.setStorageSync("home_title",result.data.wxApp)
						uni.setStorageSync("qy_home",result.data.image.file_path)
						uni.setStorageSync("firm_name",result.data.title)
						uni.setStorageSync("kefu",url)
					})
					.finally(() => this.isLoading = false)
			},
			getstatus(){
				let appid = uni.getAccountInfoSync().miniProgram.appId;
				let token = uni.getStorageSync("AccessToken")
				userApi.getstatus({
					appid,
					token
				}).then(result => {
					if(result.code === 1){
						uni.setStorageSync('Wxstatus',result.data[0]);
					}
				})
			},
			shareMp(){
				userApi.shareMp()
				.then(result => {
					uni.setStorageSync("shareMp",result.data)
				})
			},
			userInfo() {
				let _this = this;
				let token = uni.getStorageSync("AccessToken")
				userApi.userInfo({
						wxapp_id: _this.$wxapp_id,
						token:  token
					})
					.then(result => {
						if(result.code === 1){
							uni.setStorageSync('userInfo',result.data);
						}
					}).catch(err => {
			
					})
			},
		}
	}
</script>

<style lang="scss">
	@import "common/common.css";
	@import "common/main.css";
	.uni-page-head{
		max-width: 765px;
		margin: 0 auto;
	}
	page {
		max-width: 765px;
		margin: 0 auto;
		position: relative;
		// background-color: #F5F7FA;
		color: #000000;
		font-weight: 500;
	} 
	.uni-tabbar{
		max-width: 765px;
		margin: 0 auto;
	}
	.null-select{
		width: 30rpx;
		height: 30rpx;
		border: 3rpx solid #BCBCBC;
		border-radius: 100%;
		position: relative;
		&.avtive{
			background: #3fdc3c;
			text-align: center;
			border: 1.2rpx solid #3fdc3c;
			&:before{
				content: '';
				position: absolute;
				right: 10rpx;
				bottom: 8rpx;
				width: 8rpx;
				height: 16rpx;
				border-color: #ffffff;
				border-style: solid;
				border-width: 0 4rpx 4rpx 0;
				transform: rotate(45deg);
			}
		}
	}
</style>
